﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace Previewer
{
    public class CosmosProgressBar : Control
    {
        int _max = 100, _min = 0, _value = 0;
        string _progressBarText;
        Color _barColor = Color.LimeGreen;
        public int Max
        {
            get
            {
                return _max;
            }
            set
            {
                _max = value;
                Invalidate();
            }
        }
        public int Min
        {
            get
            {
                return _min;
            }
            set
            {
                _min = value;
                Invalidate();
            }
        }
        public int Value
        {
            get
            {
                return _value;
            }
            set
            {
                if (value > _max) value = _max;
                if (value < _min) value = _min;
                _value = value;
                Invalidate();
            }
        }
        public string ProgressBarText
        {
            get
            {
                return _progressBarText;
            }
            set
            {
                _progressBarText = value;
                Invalidate();
            }
        }
        public override string Text
        {
            get
            {
                return _progressBarText;
            }
            set
            {
                _progressBarText = value;
                Invalidate();
            }
        }
        public Color BarColor
        {
            get
            {
                return _barColor;
            }
            set
            {
                _barColor = value;
                Invalidate();
            }
        }

        public CosmosProgressBar()
        {
            this.Padding = new Padding(1);
            SetStyle(ControlStyles.UserMouse, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            Graphics g = e.Graphics;
            //g.Clear(this.BackColor);
            int progressSize = (int)(this.Width * ((float)Value / (float)Max));
            Rectangle progress = new Rectangle(Padding.Left + 1, Padding.Top + 1, (progressSize - Padding.Right) - (2 + Padding.Right), (this.Height - Padding.Bottom) - (2 + Padding.Bottom));
            g.DrawRectangle(new Pen(new SolidBrush(this.ForeColor)), this.ClientRectangle);
            g.FillRectangle(new SolidBrush(BarColor), progress);
            g.DrawRectangle(new Pen(this.ForeColor), progress);
            g.DrawString(this.ProgressBarText, new Font(this.Font.FontFamily, (float)8.25, FontStyle.Regular), new SolidBrush(this.ForeColor), new PointF(this.Width / 2 - 10, this.Height / 2 - 7));
        }
    }
}
